<?php 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	} else : {
?>

<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>

<style type="text/css">
   body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
  .sidebar-nav {
    padding: 9px 0;
  
  }
    .container {
    padding-top: 100px;
  }

    .finish {
        padding-top: 500px;
        padding-left: 25px;
        padding-right: 25px;

  }
  
  @media (max-width: 980px) {
    /* Enable use of floated navbar text */
    .navbar-text.pull-right {
      float: none;
      padding-left: 5px;
      padding-right: 5px;
    }
  }
</style>

</head>

<body>
<?php include ('configuration.php'); ?>
<script type="text/javascript"
	src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>


<div class="navbar navbar-inverse navbar-fixed-top">
	<div class="navbar-inner">
		<div class="container-fluid">
			<button type="button" class="btn btn-navbar" data-toggle="collapse"
				data-target=".nav-collapse">
				<span class="icon-bar"></span> <span class="icon-bar"></span> <span
					class="icon-bar"></span>
			</button>
			<a class="brand" href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
			<div class="nav-collapse collapse">
				<p class="navbar-text pull-right">
				
					<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Log out</a>
				</p>
				<ul class="nav">
					<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
					<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
					<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
				</ul>
			</div>
		</div>
	</div>
</div>

	<?php
	//hago consulta con el id q me he traido del post de modifyProject y con eso me traigo todos los datos y relleno
    $id_proj = $_SESSION["id_proj"];
	$id_ver1=$_POST['select_version1']; 
    $id_ver2=$_POST['select_version2'];   
    
    if ($id_ver1 != 0 && $id_ver2 != 0): {
	?>

<div class="span12" >
	<h1 class="muted" >Compare > </h1><h1 style="font-weight:bold"><font size="5"><u>Comparison between the versions <?php echo $id_ver1;?> and <?php echo $id_ver2;?></u></font></h1>
</div>
		
<div class="span12" >  
<table  class="table table-striped table-condensed" border="1" >
  <thead>
      <tr>
      <th>First version</th>
      <th>Second version</th>
      <th>Comparison</th>
      </tr>
  </thead>   
  <tbody>
  	<?php 
  		//Consulto las fechas de inicio de ambas versiones
  		$q1 = mysql_query("SELECT initial_date FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver1'");  	
  		$res1 = mysql_fetch_assoc($q1);
		$initial_ver1 = $res1['initial_date'];
		$q2 = mysql_query("SELECT initial_date FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver2'");  	
  		$res2 = mysql_fetch_assoc($q2);
		$initial_ver2 = $res2['initial_date'];
		$anyoIni1 = substr($initial_ver1, 0, 4);
		$anyoIni2 = substr($initial_ver2, 0, 4);
		$aux1 = substr($initial_ver1, 5, 6);
		$mesIni1 = substr($aux1, 0, 2);
		$aux2 = substr($initial_ver2, 5, 6);
		$mesIni2 = substr($aux2, 0, 2);
		$diaIni1 = substr($initial_ver1, 8, 9);
		$diaIni2 = substr($initial_ver2, 8, 9);
		
		//Consulto las fechas de fin de ambas versiones
  		$q3 = mysql_query("SELECT final_date FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver1'");  	
  		$res3 = mysql_fetch_assoc($q3);
		$final_ver1 = $res3['final_date'];
		$q4 = mysql_query("SELECT final_date FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver2'");  	
  		$res4 = mysql_fetch_assoc($q4);
		$final_ver2 = $res4['final_date'];
		$anyoFin1 = substr($final_ver1, 0, 4);
		$anyoFin2 = substr($final_ver2, 0, 4);
		$aux3 = substr($final_ver1, 5, 6);
		$mesFin1 = substr($aux3, 0, 2);
		$aux4 = substr($final_ver2, 5, 6);
		$mesFin2 = substr($aux4, 0, 2);
		$diaFin1 = substr($final_ver1, 8, 9);
		$diaFin2 = substr($final_ver2, 8, 9);
		
		//Consulto los avances de ambas versiones
  		$q5 = mysql_query("SELECT advantage FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver1'");  	
  		$res5 = mysql_fetch_assoc($q5);
		$adv_ver1 = $res5['advantage'];
		$q6 = mysql_query("SELECT advantage FROM project WHERE id_project = '$id_proj' AND id_version = '$id_ver2'");  	
  		$res6 = mysql_fetch_assoc($q6);
		$adv_ver2 = $res6['advantage'];	
		
		
		//NUMERO DE D�AS ENTRE LAS FECHAS DE INICIO
		$diasPorMes = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  		$difDiasInitial = 0;  
    	if ( ($anyoIni1 - $anyoIni2 > 0) ) {
    		$inicioMes = $mesIni2;
    		$inicioDiaMes = $diaIni2;
    		// d�as de los a�os anteriores al de la fecha inicio 1
    		for ($j = $anyoIni2; $j < $anyoIni1; $j++) {
    			for ($i = $inicioMes; $i <= 12; $i++) {
    				for ($z = $inicioDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
    					$difDiasInitial =  $difDiasInitial + 1;
    				}
    				$inicioDiaMes = 1;
    			} 
    			$inicioMes = 1;		
    		}
    		// dias del ultimo a�o de la fecha de inicio 1 y anteriores al mes de la fecha inicio 1
    		for ($i = 1; $i < $mesIni1; $i++) {
    			for ($z = 1; $z <= $diasPorMes[$i-1]; $z++) {
    				$difDiasInitial =  $difDiasInitial + 1;
    			}
    		}
    		// dias del ultimo mes de la fecha inicio 1
    		for ($z = 1; $z < $diaIni1; $z++) {
    			$difDiasInitial =  $difDiasInitial + 1;
    		}
    		$comp1 = "The initial date of the first version is ".$difDiasInitial." days later";
    		
    	} elseif ( ($anyoIni1 - $anyoIni2 == 0) ) {
    		if ($mesIni2 > $mesIni1) {
        		$inicioMes = $mesIni1;
    			$inicioDiaMes = $diaIni1;
    			$finMes = $mesIni2;	
    			$finDiaMes = $diaIni2;
    			$diaUltimoMes = 1;
    			$message = 1;
    		} elseif ($mesIni2 < $mesIni1) {
    			$inicioMes = $mesIni2;
    			$inicioDiaMes = $diaIni2;
    			$finMes = $mesIni1;
    			$finDiaMes = $diaIni1;
    			$diaUltimoMes = 1;
    			$message = 2;
    		} elseif ($mesIni2 == $mesIni1){
    			$inicioMes = 12;
    			$finMes = 11;
    			if ($diaIni1 > $diaIni2) {
    				$diaUltimoMes = $diaIni2;
    				$finDiaMes = $diaIni1;
    				$message = 3;
    			} else {
    				$diaUltimoMes = $diaIni1;
					$finDiaMes = $diaIni2;
					$message = 4;
					if ($diaIni1 == $diaIni2) $message = 5;
    			}
    		}
    		// d�as de la fecha de inicio anteriores al mes de inicio
    		for ($i = $inicioMes; $i < $finMes; $i++) {
        		for ($z = $inicioDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
        			$difDiasInitial =  $difDiasInitial + 1;
        		}
        		$inicioDiaMes = 1;
        	}
    		// dias del ultimo mes de la fecha inicio
    		for ($z = $diaUltimoMes; $z < $finDiaMes; $z++) {
    			$difDiasInitial =  $difDiasInitial + 1;
    		}
    		if ($message == 1 || $message == 4) {
    			$comp1 = "The initial date of the second version is ".$difDiasInitial." days later";
    		} elseif ($message == 2 || $message == 3){
    			$comp1 = "The initial date of the first version is ".$difDiasInitial." days later";
    		} elseif ($message == 5) {
    			$comp1 = "Initial date matches";
    		}
    		
    	} elseif ( ($anyoIni1 - $anyoIni2 < 0) ) {
    		$inicioMes = $mesIni1;
    		$inicioDiaMes = $diaIni1;
    		// d�as de los a�os anteriores al de la fecha inicio 2
    		for ($j = $anyoIni1; $j < $anyoIni2; $j++) {
    			for ($i = $inicioMes; $i <= 12; $i++) {
    				for ($z = $inicioDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
    					$difDiasInitial =  $difDiasInitial + 1;
    				}
    				$inicioDiaMes = 1;
    			} 
    			$inicioMes = 1;		
    		}
    		// dias del ultimo a�o de la fecha de inicio 2 y anteriores al mes de la fecha inicio 2
    		for ($i = 1; $i < $mesIni2; $i++) {
    			for ($z = 1; $z <= $diasPorMes[$i-1]; $z++) {
    				$difDiasInitial =  $difDiasInitial + 1;
    			}
    		}
    		// dias del ultimo mes de la fecha inicio 2
    		for ($z = 1; $z < $diaIni2; $z++) {
    			$difDiasInitial =  $difDiasInitial + 1;
    		}
    		$comp1 = "The initial date of the second version is ".$difDiasInitial." days later";
    	}	
		
    	
		//NUMERO DE D�AS ENTRE LAS FECHAS DE FIN
  		$difDiasFinal = 0;  
    	if ( ($anyoFin1 - $anyoFin2 > 0) ) {
    		$finalMes = $mesFin2;
    		$finalDiaMes = $diaFin2;
    		// d�as de los a�os anteriores al de la fecha inicio 1
    		for ($j = $anyoFin2; $j < $anyoFin1; $j++) {
    			for ($i = $finalMes; $i <= 12; $i++) {
    				for ($z = $finalDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
    					$difDiasFinal =  $difDiasFinal + 1;
    				}
    				$finalDiaMes = 1;
    			} 
    			$finalMes = 1;		
    		}
    		// dias del ultimo a�o de la fecha de inicio 1 y anteriores al mes de la fecha inicio 1
    		for ($i = 1; $i < $mesFin1; $i++) {
    			for ($z = 1; $z <= $diasPorMes[$i-1]; $z++) {
    				$difDiasFinal =  $difDiasFinal + 1;
    			}
    		}
    		// dias del ultimo mes de la fecha inicio 1
    		for ($z = 1; $z < $diaFin1; $z++) {
    			$difDiasFinal =  $difDiasFinal + 1;
    		}
    		$comp2 = "The initial date of the first version is ".$difDiasFinal." days later";
    		
    	} elseif ( ($anyoFin1 - $anyoFin2 == 0) ) {
    		if ($mesFin2 > $mesFin1) {
        		$finalMes = $mesFin1;
    			$finalDiaMes = $diaFin1;
    			$finMes = $mesFin2;	
    			$finDiaMes = $diaFin2;
    			$diaUltimoMes = 1;
    			$message = 1;
    		} elseif ($mesFin2 < $mesFin1) {
    			$finalMes = $mesFin2;
    			$finalDiaMes = $diaFin2;
    			$finMes = $mesFin1;
    			$finDiaMes = $diaFin1;
    			$diaUltimoMes = 1;
    			$message = 2;
    		} elseif ($mesFin2 == $mesFin1){
    			$finalMes = 12;
    			$finMes = 11;
    			if ($diaFin1 > $diaFin2) {
    				$diaUltimoMes = $diaFin2;
    				$finDiaMes = $diaFin1;
    				$message = 3;
    			} else {
    				$diaUltimoMes = $diaFin1;
					$finDiaMes = $diaFin2;
					$message = 4;
					if ($diaFin1 == $diaFin2) $message = 5;
    			}
    		}
    		// d�as de la fecha de inicio anteriores al mes de inicio
    		for ($i = $finalMes; $i < $finMes; $i++) {
        		for ($z = $finalDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
        			$difDiasFinal =  $difDiasFinal + 1;
        		}
        		$finalDiaMes = 1;
        	}
    		// dias del ultimo mes de la fecha inicio
    		for ($z = $diaUltimoMes; $z < $finDiaMes; $z++) {
    			$difDiasFinal =  $difDiasFinal + 1;
    		}
    		if ($message == 1 || $message == 4) {
    			$comp2 = "The final date of the second version is ".$difDiasFinal." days later";
    		} elseif ($message == 2 || $message == 3){
    			$comp2 = "The final date of the first version is ".$difDiasFinal." days later";
    		} elseif ($message == 5) {
    			$comp2 = "Final date matches";
    		}
    		
    	} elseif ( ($anyoFin1 - $anyoFin2 < 0) ) {
    		$finalMes = $mesFin1;
    		$finalDiaMes = $diaFin1;
    		// d�as de los a�os anteriores al de la fecha inicio 2
    		for ($j = $anyoFin1; $j < $anyoFin2; $j++) {
    			for ($i = $finalMes; $i <= 12; $i++) {
    				for ($z = $finalDiaMes; $z <= $diasPorMes[$i-1]; $z++) {
    					$difDiasFinal =  $difDiasFinal + 1;
    				}
    				$finalDiaMes = 1;
    			} 
    			$finalMes = 1;		
    		}
    		// dias del ultimo a�o de la fecha de inicio 2 y anteriores al mes de la fecha inicio 2
    		for ($i = 1; $i < $mesFin2; $i++) {
    			for ($z = 1; $z <= $diasPorMes[$i-1]; $z++) {
    				$difDiasFinal =  $difDiasFinal + 1;
    			}
    		}
    		// dias del ultimo mes de la fecha inicio 2
    		for ($z = 1; $z < $diaFin2; $z++) {
    			$difDiasFinal =  $difDiasFinal + 1;
    		}
    		$comp2 = "The initial date of the second version is ".$difDiasFinal." days later";
    	}
		
    	
		//DIFERENCIA ENTRE LOS AVANCES
		$difAvances = 0;  
    	if ( ($adv_ver1 - $adv_ver2 > 0) ) {
    		$difAvances = $adv_ver1 - $adv_ver2;
    		$comp3 = "The advance of the first version is a ".$difAvances."% bigger than the advance of the second version";
    		
    	} elseif ( ($adv_ver1 - $adv_ver2 == 0) ) {
    		$comp3 = "The advance matches";
    		
    	} elseif ( ($adv_ver1 - $adv_ver2 < 0) ) {
    		$difAvances = $adv_ver2 - $adv_ver1;
    		$comp3 = "The advance of the second version is a ".$difAvances."% bigger than the advance of the first version";
    	}
		

  	
  		echo "<tr>";
        echo "<td>" . $id_ver1 . "</td>";
        echo "<td>" . $id_ver2 . "</td>";
        echo "<td>" . "-" . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>" . "Initial_date: ". $initial_ver1 . "</td>";
        echo "<td>" . "Initial_date: ". $initial_ver2 . "</td>";
        echo "<td>" . $comp1 . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>" . "Final_date: ". $final_ver1 . "</td>";
        echo "<td>" . "Final_date: ". $final_ver2 . "</td>";
        echo "<td>" . $comp2 . "</td>";
        echo "</tr>";
    	echo "<tr>";
        echo "<td>" . "Advance: ". $adv_ver1 ."%". "</td>";
        echo "<td>" . "Advance: ". $adv_ver2 ."%". "</td>";
        echo "<td>" . $comp3 . "</td>";
        echo "</tr>";
  	?>
  </tbody>
</table>
</div>

<div class="finish">	
	<a class="btn btn-primary pull-left" type="button" href="http://localhost/bases--grupo10/compare2_restore.php?" >Back</a>
    <a class="btn btn-primary pull-right" type="button" href="http://localhost/bases--grupo10/homeAdmin.php?" >Finish</a>  
</div>  
  			
	<?php
    } else: {
    	echo "<h1 style='position:absolute;top:130px;left:50px;font-weight:bold'><font size='5'><u> You must select two versions for make the comparison</u></font></h1>"; 
    	echo "<div class='finish'>";
    	echo "<a class='btn btn-primary' type='button' href='http://localhost/bases--grupo10/compare2_restore.php?'>Back</a>";
		echo "</div>  ";
    }endif;		
	?> 
 	
</body>
</html>

<?php }endif;?>